/******************************************************************************/
/*                                                                            */
/*  Copyright (c) 2009: ngi-central.org                                       */
/*  All rights reserved.                                                      */
/*                                                                            */
/******************************************************************************/


/// \file
/// This file implements the box geometric primitive.

#ifndef __sgl_box_h__
#define __sgl_box_h__

#include <sgl/global_settings.h>
#include <sgl/conditions.h>
#include <sgl/geometric_primitive.h>
#include <sgl/point.h>
#include <sgl/vector.h>
#include <boost/static_assert.hpp>
#include <boost/operators.hpp>
#include <boost/concept_check.hpp>

namespace sgl {


/******************************************************************************/
/*                                                                            */
/* box                                                                        */
/*                                                                            */
/******************************************************************************/

template <
	/// The dimensionality. 
	//!
	//! This defaults to two dimensions.
	size_t D=2,
	/// The element type. 
	//! 
	//! This type defaults to SGL_FPTYPE (which in turn defaults to double), 
	//! but any other numeric type is also suitable.
	typename T=SGL_FPTYPE>
/// A box in euclidean space.
//!
//! A box represents an axis-parallel box. It has an origin and a size.
class box
	/// A box is a geometric primitive.
	: public geometric_primitive<D,T>
	, boost::equality_comparable<sgl::box<D,T>, sgl::box<D,T>
	, boost::additive<sgl::box<D,T>, sgl::vector<D,T>
	, boost::multiplicative<sgl::box<D,T>, T
	> > >
{
	// A box must have two or more dimensions.
	BOOST_STATIC_ASSERT(D >= 2);

public:
	/// Default constructor.
	//!
	//! Constructs a zero box.
	box()
		: origin_()
		, size_()
	{ }

	/// Construct a box from a point and a vector.
	box(
		/// The origin of the box.
		sgl::point<D,T> origin,
		/// The size of the box.
		sgl::vector<D,T> size)
		: origin_(origin)
		, size_(size)
	{ }

	/// Construct a box from two points.
	box(
		/// The origin of the box.
		sgl::point<D,T> from,
		/// The second corner of the box.
		sgl::point<D,T> to)
		: origin_(from)
		, size_(to - from)
	{ }

	/// Copy constructor.
	box(
		/// The right hand side.
		sgl::box<D,T> const& rhs)
		: origin_(rhs.origin_)
		, size_(rhs.size_)
	{ }

	/// Copy assignment operator.
	sgl::box<D,T> & operator = (
		/// The right hand side.
		sgl::box<D,T> rhs)
	{
		swap(rhs);
		return *this;
	}

	/// Exchanges the elements of two box objects.
	void swap(
		/// The other box.
		sgl::box<D,T> & rhs)
	{
		SGL_PRECONDITION(D == rhs.dimensions(),
			"dimension mismatch.");

		std::swap(origin_, rhs.origin_);
		std::swap(size_, rhs.size_);
	}

	/// Find the type of the geometric primitive.
	//!
	//! /return The type of the primitive.
	geometric_type type() const
	{
		return sgl::geometric_primitive<D,T>::box;
	} 

	/// Access the origin of the box.
	//!
	//! /return Returns a reference to the origin of the box.
	sgl::point<D,T> & origin()
	{
		return origin_;
	}

	/// Access the origin of the box.
	//!
	//! /return Returns a const reference to the origin of the box.
	sgl::point<D,T> const & origin() const
	{
		return origin_;
	}

	/// Access the size of the box.
	//!
	//! /return Returns a reference to the size of the box.
	sgl::vector<D,T> & size()
	{
		return size_;
	}

	/// Access the size of the box.
	//!
	//! /return Returns a const reference to the size of the box.
	sgl::vector<D,T> const & size() const
	{
		return size_;
	}

	/// Unary negation.
	//!
	//! A box is negated by negating its size while leaving its origin
	//! untouched.
	//!
	//! \return A negated box object.
    sgl::box<D,T> operator - () const
    {
        return sgl::box<D,T>(origin_, -size_);
    }

	/// Equality operator.
	//!
	//! Two boxes are compared for equality by comparing their origin and 
	//! size in a pairwise manner. The boxes are equal only if all pairwise 
	//! comparisons are equal.
	//!
	//! Through the use of boost::equality as a base class operator != is 
	//! defined as well and implemented in terms of this operator ==.
	//!
	//! \return true if equal, false otherwise.
    bool operator == (
		/// Right hand side.
		sgl::box<D,T> const& rhs) const
    {
		SGL_PRECONDITION(D == rhs.dimensions(),
			"dimension mismatch.");

		return 
			origin_ == rhs.origin_ &&
			size_ == rhs.size_;
    }

	/// Add a vector to a box.
	//!
	//! Adding a vector to a box moves the box in direction of the vector.
	//! Addition is implemented by pairwise addition of the components of the 
	//! boxes origin and the vector.
	//!
	//! Through the use of boost::additive as a base class operator + is 
	//! defined as well and implemented in terms of this operator +=.
	//!
	//! \return A reference to this object.
    sgl::box<D,T> & operator += (
		/// Right hand side.
		sgl::vector<D,T> const& rhs)
    {
		SGL_PRECONDITION(D == rhs.dimensions(),
			"dimension mismatch.");

		origin_ += rhs;

        return *this;
    }

	/// Subtract a vector from a box.
	//!
	//! Subtracting a vector from a box moves the box in the opposite 
	//! direction of the vector. Subtraction is implemented by pairwise 
	//! subtraction of the components of the boxes origin and the vector.
	//!
	//! Through the use of boost::additive as a base class operator - is 
	//! defined as well and implemented in terms of this operator -=.
	//!
	//! \return A reference to this object.
    sgl::box<D,T> & operator -= (
		/// Right hand side.
		sgl::vector<D,T> const& rhs)
    {
		SGL_PRECONDITION(D == rhs.dimensions(),
			"dimension mismatch.");

		origin_ -= rhs;

		return *this;
    }

	/// Scale a box by multiplication.
	//!
	//! A box is scaled by multiplying its size with the factor.
	//!
	//! Through the use of boost::multiplicative as a base class operator * is 
	//! defined as well and implemented in terms of this operator *=.
	//!
	//! \return A reference to this object.
	sgl::box<D,T> & operator *= (
		/// The scaling factor.
		T const& factor)
	{
		size_ *= factor;

		return *this;
	}

	/// Scale a box by division.
	//!
	//! A box is scaled by dividing its size through the divisor.
	//!
	//! Through the use of boost::multiplicative as a base class operator / is 
	//! defined as well and implemented in terms of this operator /=.
	//!
	//! \return A reference to this object.
	sgl::box<D,T> & operator /= (
		/// The scaling factor.
		T const& divisor)
	{
		size_ /= divisor;

		return *this;
	}

private:
	// The starting point of the box.
	sgl::point<D,T> origin_;

	/// The size of the box.
	sgl::vector<D,T> size_;
};


/// A two-dimensional box with the default component type.
typedef sgl::box<2> box2;

/// A two-dimensional box with float component type.
typedef sgl::box<2, float> box2f;

/// A two-dimensional box with double component type.
typedef sgl::box<2, double> box2d;


/// A three-dimensional box with the default component type.
typedef sgl::box<3> box3;

/// A three-dimensional box with float component type.
typedef sgl::box<3, float> box3f;

/// A three-dimensional box with double component type.
typedef sgl::box<3, double> box3d;


} // namespace sgl

#endif // __sgl_box_h__
